package game.editer.history;

/**
 * 编辑器历史记录数据，用于实现撤销、重做的重要数据
 */
class HistoryData {
	/**
	 * 最大可缓存的历史记录数量，默认为`2500`个
	 */
	public var maxSize(default, set):Int = 2500;

	public var data:Array<IHistoryAction> = [];

	public var redoData:Array<IHistoryAction> = [];

	public var redoing = false;

	private function set_maxSize(value:Int):Int {
		this.maxSize = value;
		return maxSize;
	}

	public function new() {}

	/**
	 * 追加执行历史记录
	 * @param action 
	 */
	public function todo(action:IHistoryAction):Void {
		if (redoing)
			return;
		redoing = true;
		data.push(action);
		trace("添加历史记录：", Type.getClassName(Type.getClass(action)), data.length);
		if (redoData.length > 0)
			redoData.splice(0, redoData.length);
		redoing = false;
	}

	/**
	 * 撤销
	 */
	public function undo():Void {
		redoing = true;
		if (data.length > 0) {
			var v = data.pop();
			v.undo();
			redoData.push(v);
		}
		redoing = false;
	}

	/**
	 * 重做
	 */
	public function redo():Void {
		if (redoData.length > 0) {
			var v = redoData.pop();
			todo(v);
		}
	}
}
